﻿using BLL;
using DAL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WeChat
{
    /// <summary>
    /// WaterReserveHandler 的摘要说明
    /// </summary>
    public class WaterReserveHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            string rtnJson = string.Empty;
            WaterReserveBLL wrb = new WaterReserveBLL();
            string funcName = context.Request.Form["funcName"];

            switch (funcName)
            {
                case "addReservedRecords":
                    string rfkh = context.Request.Form["rfkh"];
                    //string bookDate = context.Request.Form["bookDate"];
                    string beginTime = context.Request.Form["beginTime"];
                    string endTime = context.Request.Form["endTime"];
                    string wz = context.Request.Form["wz"];
                    string reservedCode =  CreateRegionCode(6);
                    string orderId = GetRoundNO()+ reservedCode;
                    while (wrb.isExistReservedCode(wz, reservedCode).Rows.Count > 0)
                    {
                        reservedCode = CreateRegionCode(6);
                        orderId = GetRoundNO() + reservedCode;
                        ApplicationLog.Write($"重复的预约码={reservedCode},预约区域={wz}");

                    }


                    if (wrb.AddWaterReserveRecords(rfkh,   beginTime,   endTime, wz, reservedCode, "1", orderId) > 0)
                    {
                      var dt=  wrb.getJh(wz);
                        if (dt.Rows.Count>0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                var jh = dt.Rows[i]["jh"].ToString();
                                wrb.AddWaterReserveOffRecords(rfkh, beginTime, endTime, jh, reservedCode, "0", orderId);
                            }
                        }
                     
                      
                        rtnJson = "10000";//成功
                    }
                    else
                    {
                        rtnJson = "10005";//失败
                            
                    }

                    break;

                case "cancel":
                    rfkh = context.Request.Form["rfkh"];
                    endTime = context.Request.Form["etime"];
                    wz = context.Request.Form["wz"];
                    reservedCode = context.Request.Form["code"];

                    if (Convert.ToDateTime(endTime) >=DateTime.Now)
                    {
                        if (wrb.CancelWaterReserveRecords(rfkh, endTime, wz, reservedCode) > 0)
                        {
                            
                            rtnJson = "10000";//成功
                        }
                        else
                        {
                            rtnJson = "10005";//失败

                        }

                    }
                    else
                    {
                        rtnJson = "10006";//已到预约时间不可取消
                    }

                  






                    break;

            }







            context.Response.ContentType = "text/plain";
            context.Response.Write(rtnJson);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }



        public string CreateRegionCode(int strlength)
        {
            string so = "1,2,3,4,5,6,7,8,9,0";
            string[] strArr = so.Split(',');
            string code = "";
            Random rand = new Random();
            for (int i = 0; i < strlength; i++)
            {
                code += strArr[rand.Next(0, strArr.Length)];
            }
            return code;
        }
        public static string GuidToLongID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            string guid = BitConverter.ToInt64(buffer, 0).ToString();
            string str = new string(guid.Reverse().Take(6).Reverse().ToArray());
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            DateTime nowTime = DateTime.Now;
            string unixTime = Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero).ToString();
            string strs = new string(unixTime.Reverse().Take(6).Reverse().ToArray());
            return str + strs;
        }

        public static string GetRoundNO() 
        {
            string str = string.Empty;
            str = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            return str;
        }

    }
}